| string (1) | basic_string& insert (size_type pos, const basic_string& str); |
|---|---|
| substring (2) | basic_string& insert (size_type pos, const basic_string& str, size_type subpos, size_type sublen); |
| c-string (3) | basic_string& insert (size_type pos, const charT* s); |
| buffer (4) | basic_string& insert (size_type pos, const charT* s, size_type n); |
| fill (5) | basic_string& insert (size_type pos, size_type n, charT c); void insert (iterator p, size_type n, charT c); |
| single character (6) | iterator insert (iterator p, charT c); |
| range (7) | template <class InputIterator> void insert (iterator p, InputIterator first, InputIterator last); |
| string (1) | basic_string& insert (size_type pos, const basic_string& str); |
|---|---|
| substring (2) | basic_string& insert (size_type pos, const basic_string& str, size_type subpos, size_type sublen); |
| c-string (3) | basic_string& insert (size_type pos, const charT* s); |
| buffer (4) | basic_string& insert (size_type pos, const charT* s, size_type n); |
| fill (5) | basic_string& insert (size_type pos, size_type n, charT c); iterator insert (const_iterator p, size_type n, charT c); |
| single character (6) | iterator insert (const_iterator p, charT c); |
| range (7) | template <class InputIterator> iterator insert (iterator p, InputIterator first, InputIterator last); |
| initializer list (8) | basic_string& insert (const_iterator p, initializer_list<charT> il); |
| string (1) | basic_string& insert (size_type pos, const basic_string& str); |
|---|---|
| substring (2) | basic_string& insert (size_type pos, const basic_string& str, size_type subpos, size_type sublen = npos); |
| c-string (3) | basic_string& insert (size_type pos, const charT* s); |
| buffer (4) | basic_string& insert (size_type pos, const charT* s, size_type n); |
| fill (5) | basic_string& insert (size_type pos, size_type n, charT c); iterator insert (const_iterator p, size_type n, charT c); |
| single character (6) | iterator insert (const_iterator p, charT c); |
| range (7) | template <class InputIterator> iterator insert (iterator p, InputIterator first, InputIterator last); |
| initializer list (8) | basic_string& insert (const_iterator p, initializer_list<charT> il); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// inserting into a string
#include <iostream>
#include <string>
int main ()
{
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
std::cout << str << '\n';
return 0;
}
to be, or not to be: that is the question...